import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import viteCompression from 'vite-plugin-compression'

const path = require("path");
const TimeStamp = new Date().getTime();
export default defineConfig({
  publicDir: 'public',
  // 去除console
  terserOptions: {
    compress: {
      drop_console: true,
      drop_debugger: true
    }
  },
  resolve: {
    alias: {
      '@': path.join(__dirname, 'src'), // 设置 `@` 指向 `src` 目录
      '@public': path.join(__dirname, 'public') // 设置 `@` 指向 `src` 目录
    }
  },
  server: {
    host: true, // 默认为localhost
    port: 9001, // 端口号
    open: true, // 是否自动打开浏览器
    base: './', // 生产环境路径
    strictPort: true,
    proxy: {
      // 本地开发环境通过代理实现跨域，生产环境使用 nginx 转发
      '^/api': {
        target: 'http://xxxx:xxxx', // 后端服务实际地址
        changeOrigin: true
        // rewrite: () => path.replace(/^\/api/, '')
      },
    }
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets/js',
    sourcemap: false,
    rollupOptions: {
      output: {
        manualChunks: {
          'element-plus': ['element-plus']
        },
        assetFileNames: `assets/[ext]/[name]-[hash]-${TimeStamp}.[ext]`,
        chunkFileName: `assets/js/[name]-[hash]-${TimeStamp}.js`,
        entryFileNames: `assets/js/[name]-[hash]-${TimeStamp}.js`,
      }
    }
  },
  optimizeDeps: {
    exclude: [],
    include: []
  },
  css: {
    preprocessorOptions: {
      // scss: {
      //   modifyVars: {},
      //   javascriptEnabled: true
      // }
    }
  },
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
    viteCompression({
      // 开启gzip模式
      verbose: true,
      disable: false,
      threshold: 10240,
      algorithm: 'gzip',
      ext: '.gz'
    })],

})
